package com.android.lovingpet.util;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;

import com.android.lovingpet.bean.JsonIds;
import com.android.lovingpet.common.Back;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CallbackStore {

    public static abstract class CallbackImpl<T extends Back> implements Callback {

        public T back;

        public CallbackImpl(T back) {
            this.back = back;
        }

        public abstract void onResponse(String json) throws JSONException;

        @Override
        public void onFailure(Call call, IOException e) {
            back.onFailure(HttpUtil.getFailureMeg());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String str = response.body().string();
            RequestBody body = call.request().body();
            if (body instanceof FormBody) {
                FormBody formBody = (FormBody) body;
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < formBody.size(); i++) {
                    builder.append('-').append(formBody.name(i)).append('-').append(formBody.value(i)).append('-');
                }
                Log.d("来自CallbackImpl", str + "URL：" + call.request().url() + "参数：" + builder + "body" + body.toString());
            }

                try {
                JSONObject object = new JSONObject(str);
                int status = object.getInt("code");
                if (status != 0) {
                    back.onFailure(MyHttp.getCodeString(status));
                } else {
                    onResponse(str);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ;
        }
    }

    public static class CallbackNon<T extends Back> extends CallbackImpl<T> {

        public CallbackNon(T back) {
            super(back);
        }

        @Override
        public void onResponse(String json) {
            back.onSuccess(null);
        }
    }

    public static class CallbackId<T extends Back> extends CallbackImpl<T> {

        public CallbackId(T back) {
            super(back);
        }

        @Override
        public void onResponse(String json) {
            try {
                JSONObject object = new JSONObject(json);
                Bundle bundle = new Bundle();
                bundle.putInt("id", object.getInt("id"));
                back.onSuccess(bundle);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static class CallbackIds<T extends Back> extends CallbackImpl<T> {

        public CallbackIds(T back) {
            super(back);
        }

        @Override
        public void onResponse(String json) throws JSONException {
            JsonIds jsonIds = new Gson().fromJson(json, JsonIds.class);
            Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("ids", jsonIds.getIds());
            back.onSuccess(bundle);
        }
    }
}
